﻿using System;
using System.Collections.Generic;
using System.Text;
using Xeml.Document;

namespace Xeml.Sampling
{
    public class PositionColumn : Column
    {
        private VariableTerm _term;
        private string _context;
        private ParameterColumnLevel _columnLevel;

        public ParameterColumnLevel ColumnLevel
        {
            get { return _columnLevel; }
            set
            {
                _columnLevel = value;
                SetNames();
            }
        }


        public string Context
        {
            get { return _context; }
            set { _context = value; SetNames(); }
        }


        public VariableTerm Term
        {
            get { return _term; }
            set
            {
                _term = value;
                SetNames();
            }
        }

        private void SetNames()
        {

            if (_term != null)
            {
                switch (_columnLevel)
                {
                    case ParameterColumnLevel.Parameter:
                        {
                            base.name = _term.TermId;
                            StringBuilder sb = new StringBuilder();

                            sb.AppendFormat("NsAlias: {0}\r\n", _term.NameSpaceAlias);
                            sb.AppendFormat("Name: {0}\r\n", _term.Name);
                            base.DisplayName = sb.ToString();
                            base.NodeDisplayName = _term.Name;
                            break;
                        }
                    case ParameterColumnLevel.Context:
                        {
                            base.name = _term.Name + ":" + _context;
                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat("Name: {0}\r\n", _term.Name);
                            sb.AppendFormat("Context: {0}", _context);
                            base.DisplayName = sb.ToString();
                            base.NodeDisplayName = _context;
                            break;
                        }

                    default:
                        break;
                }

            }
        }



        public PositionColumn()
        {
        }

        public override object ExtractRowValue(object extract)
        {

            if (!(extract is Sample))
                throw new ArgumentException("Argument type is not Sample");

            Sample s = extract as Sample;
            List<string> vals = new List<string>();
            foreach (Partition part in s.Partitions)
            {
                VariableTerm pos = part.Positions.Find(_term.TermId) as VariableTerm;
                if (pos != null)
                {
                    Value val = pos.Values.Find(_context);
                    if (val != null)
                        vals.Add(val.Val);
                }
            }

            StringBuilder sb = new StringBuilder();
            foreach (string i in vals)
            {
                sb.AppendFormat("{0}\r\n", i);

            }
            if (sb.Length > 1)
                sb.Remove(sb.Length - 2, 2);
            return sb.ToString();
        }

    }
}
